﻿using System.Collections.Generic;
using System.Linq;
using MyFloridaHouse.libSvDTO;
using System;

namespace MyFloridaHouse.DataAccess
{
    public static class DAL_Global
    {
        public static List<LayerDTO> TableLayers;
        public static List<DistrictPlanTypeDTO> TableDistrictPlanTypes;
        public static Dictionary<int, List<DataDictionaryDTO>> TableLayerDictionaries;  // hold data dictionary by layerID

        public static void InitializeLocalStorageOfDatabaseLookupTables()
        {
            var dal = new DAL_CensusData();

            DAL_Global.TableLayers = dal.GetLayers();
            DAL_Global.TableDistrictPlanTypes = dal.GetDistrictPlanTypes();
            DAL_Global.TableLayerDictionaries = new Dictionary<int, List<DataDictionaryDTO>>();
            foreach (var layer in DAL_Global.TableLayers)
            {
                DAL_Global.TableLayerDictionaries.Add(layer.layerid, dal.GetLayerDataDictionary(layer.layerid));
            }
        }

        public static string GetRegionTableNameFromLayerID(int layerID)
        {
            return DAL_Global.GetPrefixFromLayerID(layerID) + "Regions";
        }
        //public static string GetDataTableNameFromLayerID(int layerID)
        //{
        //    return DAL_Global.GetPrefixFromLayerID(layerID) + "Data";
        //}

        public static string GetPrefixFromLayerID(int layerID)
        {
            try
            {
                var prefix = (from lyr in TableLayers
                              where lyr.layerid == layerID
                              select lyr.prefix).First();
                if (prefix != null)
                {
                    return prefix;
                }
                else
                {
                        throw new Exception("Error converting layerID to prefix for layerID [" + layerID + "]");
                }
            }
            catch
            {
                //TODO: fix Kludge: Need indicator in layers table for non district build shown but available layers like interstates, highways and shorelines
                // will need to alter code that loads layer table to bring all of them, then just populate the layers list that drives the display of layers differently.
                // could not do this without coordinating with live database so put in this kludge
                if (25 == layerID)
                {
                    return "tl10stateroads10";
                }
                else if (26 == layerID)
                {
                    return "tl10expressways10";
                }
                else if (27 == layerID)
                {
                    return "tl10shoreline10";
                }
                else
                {
                    throw new Exception("Error converting layerID to prefix for layerID [" + layerID + "]");
                }
            }
        }

        public static int GetDisplayDictionaryIDFromLayerID(int layerID)
        {
            var displayDictionaryID = (from lyr in TableLayers
                          where lyr.layerid == layerID
                          select lyr.displayDictionaryID).First();
            if (displayDictionaryID != null)
            {
                return displayDictionaryID;
            }
            else
            {
                throw new Exception("layer not found for ID [" + layerID + "]");
            }
        }

        public static bool IsConfigLayer(int layerID)
        {
            var isConfigLayer = (from lyr in TableLayers
                          where lyr.layerid == layerID
                          select lyr.isConfigLayer).First();
            if (isConfigLayer != null)
            {
                return isConfigLayer;
            }
            else
            {
                return false;
            }
        }

        public static string GetDictionaryFieldName(int layerID, int dictionaryID)
        {
            var fieldName = (from dict in TableLayerDictionaries[layerID]
                                where dict.dictionaryID == dictionaryID
                                select dict.fieldName).First();
            if (fieldName != null)
            {
                return fieldName;
            }
            else
            {
                return "FieldNameNotFound";
            }
        }
    }
}
